package com.coda.clouddisk.service.impl;

import com.coda.clouddisk.entity.KesunReturn;
import com.coda.clouddisk.entity.User;
import com.coda.clouddisk.exception.BaseException;
import com.coda.clouddisk.mapper.SuperMapper;
import com.coda.clouddisk.mapper.UserMapper;
import com.coda.clouddisk.message.MessageContext;
import com.coda.clouddisk.properties.JwtProperties;
import com.coda.clouddisk.service.SuperService;
import com.coda.clouddisk.service.inter.UserService;
import com.coda.clouddisk.utils.EntityToMap;
import com.coda.clouddisk.utils.JWTUtlis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends SuperService implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    //设置mapper层对象
    @Override
    public SuperMapper getMapper() {
        return userMapper;
    }

    /**
     * 用户登录
     */
    @Override
    public String login(User user) {

        //判断是否有用户名
        if (user.getNumber() == null) {
            throw new BaseException(MessageContext.ERROR_CODE, "用户名为空");
        }

        //判断是否有密码
        if (user.getPassword() == null) {
            throw new BaseException(MessageContext.ERROR_CODE, "密码为空");
        }

        //对象转集合
        Map<String, Object> map = null;
        try {
            map = EntityToMap.convertObjectToMap(user);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        //根据用户名查询用户
        User userInfo = (User) userMapper.find(map);

        if (userInfo == null) {
            throw new BaseException(MessageContext.ERROR_CODE, "用户名或密码有误");
        }
        //将请求中的验证码进行md5加密
        String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!(userInfo.getNumber().equals(user.getNumber()) && userInfo.getPassword().equals(pwd))) {
            throw new BaseException(MessageContext.ERROR_CODE, "用户名或密码有误");
        }

        //判断用户当前状态
        if (userInfo.getStatus().equals("禁用")) {
            throw new BaseException(MessageContext.ERROR_CODE, "当前用户已禁用");
        }


        //创建jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("Id", userInfo.getId());
        String token = JWTUtlis.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);

        //将token保存在redis中
        redisTemplate.opsForValue().set(userInfo.getId(),token,720000000, TimeUnit.SECONDS);
        return token;
    }

    /**
     * 注册
     */
    @Override
    public KesunReturn Register(User user) {
        //判断是否有用户名
        if (user.getNumber() == null) {
            throw new BaseException(MessageContext.ERROR_CODE, "用户名为空");
        }

        //判断是否有密码
        if (user.getPassword() == null) {
            throw new BaseException(MessageContext.ERROR_CODE, "密码为空");
        }

        //对象转集合
        Map<String, Object> map = null;
        try {
            map = EntityToMap.convertObjectToMap(user);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        User dbUser = (User) userMapper.find(map);
        if (dbUser != null) {
            throw new BaseException(MessageContext.ERROR_CODE, "用户已经存在");
        }
        map.put("status","正常");
        map.put("password",DigestUtils.md5DigestAsHex(map.get("password").toString().getBytes()));
        int result = userMapper.add(map);
        if (!(result != 0)){
            throw new BaseException(MessageContext.ERROR_CODE,"系统异常注册失败");
        }
        KesunReturn back = new KesunReturn();
        back.setMsg("注册成功");
        back.setCode(MessageContext.SUCCESS_CODE);
        return back;
    }


}
